package com.education.api.controller.admin.education;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.education.business.service.education.TopicInfoDetailService;
import com.education.business.service.education.TopicInfoService;
import com.education.common.model.PageInfo;
import com.education.common.utils.Result;
import com.education.model.dto.TopicContentDTO;
import com.education.model.dto.TopicDTO;
import com.education.model.entity.TopicInfoDetailEntity;
import com.education.model.entity.TopicInfoEntity;
import com.education.model.request.PageParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/system/topic")
public class TopicInfoController {

    @Resource
    private TopicInfoService topicInfoService;
    @Resource
    private TopicInfoDetailService topicInfoDetailService;

    /**
     * 学生端/机构端 - 创建主题
     *
     * @return
     */
    @PostMapping
    public Result createTopic(@RequestBody TopicDTO dto) {
        topicInfoService.createTopic(dto);
        return Result.success();
    }

    /**
     * 主题回复:学生老师均可回复
     *
     * @return
     */
    @PostMapping("/reply")
    public Result reply(@RequestBody TopicDTO dto) {
        topicInfoService.replyTopic(dto);
        return Result.success();
    }

    /**
     * 后端 - 主题列表
     *
     * @param pageParam
     * @return
     */
    @GetMapping("/sys/list")
//    @RequiresPermissions("system:topic:arg")
    public Result systemList(PageParam pageParam, String courseName) {
        PageInfo<TopicContentDTO> restDto = topicInfoService.selectPageList(pageParam, courseName);
        return Result.success(restDto);
    }

    /**
     * 后端 - 列表进入查看页面
     *
     * @param id
     * @return
     */
    @GetMapping("/detail/{id}")
//    @RequiresPermissions("system:topic:arg")
    public Result systemDetail(@PathVariable("id") Integer id) {
        return Result.success(topicInfoService.selectContentList(new PageParam(), id, null));
    }

    /**
     * 后端 - 列表或者详情主题节点隐藏
     *
     * @param id
     * @return
     */
    @GetMapping("/hide/{id}/{status}")
//    @RequiresPermissions("system:topic:arg")
    public Result systemHide(@PathVariable("id") Integer id,
                             @PathVariable("status") Integer status) {
       topicInfoService.update(
                Wrappers.lambdaUpdate(TopicInfoEntity.class)
                        .set(TopicInfoEntity::getStatus, status)
                        .eq(TopicInfoEntity::getId, id));
        return Result.success();
    }

    /**
     * 后端 - 列表或者详情主题节点删除
     *
     * @param id
     * @return
     */
    @GetMapping("/remove/{id}")
//    @RequiresPermissions("system:topic:arg")
    public Result systemRemove(@PathVariable("id") Integer id) {
        topicInfoService.removeById(id);
        topicInfoDetailService.remove(
                Wrappers.lambdaQuery(TopicInfoDetailEntity.class)
                        .eq(TopicInfoDetailEntity::getTopicId, id)
        );
        return Result.success("删除成功");
    }

    /**
     * 后端 - 回复节点隐藏
     *
     * @param id
     * @param subId
     * @return
     */
    @GetMapping("/hide/{id}/{subId}/{status}")
//    @RequiresPermissions("system:topic:arg")
    public Result systemSubHide(@PathVariable("id") Integer id,
                                @PathVariable("subId") Integer subId,
                                @PathVariable("status") Integer status) {
        topicInfoDetailService.update(
                Wrappers.lambdaUpdate(TopicInfoDetailEntity.class)
                        .set(TopicInfoDetailEntity::getStatus, status)
                        .eq(TopicInfoDetailEntity::getId, subId));
        return Result.success();
    }

    /**
     * 后端 - 回复节点删除
     *
     * @param id
     * @param subId
     * @return
     */
    @GetMapping("/remove/{id}/{subId}")
//    @RequiresPermissions("system:topic:arg")
    public Result systemSubRemove(@PathVariable("id") Integer id, @PathVariable("subId") Integer subId) {
        topicInfoDetailService.removeById(subId);
        return Result.success("删除成功");
    }


}
